package com.link.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RuntimeUtil;
import com.link.service.GitPullService;
import com.link.utils.ExecuteCmd;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName GitPullServiceImpl
 * @Description TODO
 * @Author linK
 * @Date 2023/12/6 9:02
 * @Version 1.0
 **/
@Service
public class GitPullServiceImpl implements GitPullService {
    /**
     *
     */
    @Value("${git_dir_path}")
    String gitDirPath;
    @Value("${git_log_dir_path}")
    String gitLogDirPath;
    String spec = ",";

    @Override
    public String gitPull() {
        List<String> fileStr = new ArrayList<>();
        String temp = gitDirPath;
        if (gitDirPath.contains(spec)) {
            String[] split = gitDirPath.split(spec);
            for (String dir : split) {
                if (StringUtils.hasText(dir)) {
                    fileStr.add(dir);
                    addDirList(fileStr, dir);
                }
            }
        } else {
            fileStr.add(temp);
            addDirList(fileStr, temp);
        }
        StringBuilder sb = new StringBuilder();
        if (!CollectionUtils.isEmpty(fileStr)) {
            for (String path : fileStr) {
                String get = ExecuteCmd.execGit(path);
                System.out.println(get);
                sb.append(get);
            }
        }
        //Date date = new Date();
        // 去掉文件名中的事件空格
        //String dateStr = DateUtil.formatDateTime(date);
        //String dateStr = DatePattern.CHINESE_DATE_TIME_FORMAT.format(date);
        //String path = gitLogDirPath + "gitLog-" + dateStr + ".txt";
        String path = gitLogDirPath + "gitLog.txt";
        //System.out.println(sb);
        System.out.println("path = " + path);
        File file = new File(path);
        // 输出到日志文件  启用追加模式
        FileUtil.writeBytes(sb.toString().getBytes(StandardCharsets.UTF_8), file, 0, sb.toString().getBytes(StandardCharsets.UTF_8).length, true);
        //String get = ExecuteCmd.execGit(gitLogDirPath);
        // 执行不了
        //String get = ExecuteCmd.exec(gitPUll);
        //String pull = ExecuteCmd.execute(gitPUll);
        return sb.toString();
    }

    private void addDirList(List<String> fileStr, String dirPath) {
        File file = new File(dirPath);
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            if (files != null) {
                // 不包含  .  开头的文件
                fileStr.addAll(Arrays.stream(files).filter(File::isDirectory).filter(r -> !r.getName().startsWith(".")).map(File::getAbsolutePath).collect(Collectors.toList()));
            }
        }
    }


}
